package org.cy.evolution.net.http.converter.string;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.ResponseBody;
import okio.BufferedSource;
import okio.Okio;
import retrofit2.Converter;

public class StringResponseBodyConverter implements Converter<ResponseBody, String> {
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    @Override
    public String convert(ResponseBody value) throws IOException {
        try {
            BufferedSource source = Okio.buffer(Okio.source(value.byteStream()));
            return source.readString(UTF_8);
        } finally {
            value.close();
        }
    }
}
